/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufpr.tads.ghtads.model;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author G0006185
 */
public class MateriaDao {
    private final Connection connection;
    private final String sqlinsert = "insert into materia(nome,carga)values(?,?)";
    private final String sqllist = "select * from materia";
    private final String sqldel = "delete  from materia where id = ?";
    
    public MateriaDao() throws IOException, FileNotFoundException, ClassNotFoundException, SQLException {
        this.connection = new ConnectionFactory().getConnection();
    }
    public void addMateria(MateriaDto materia) throws SQLException{
        PreparedStatement stmt = connection.prepareStatement(sqlinsert);
        stmt.setString(1,materia.getNome() );
        stmt.setInt(2, materia.getCarga());
        stmt.execute();
        stmt.close();      
    }
    public List<MateriaDto> listMateria() throws SQLException{
        PreparedStatement stmt = connection.prepareStatement(sqllist);
         ResultSet rs = stmt.executeQuery();
         List<MateriaDto> listMat = new ArrayList();
         while(rs.next()){
            MateriaDto mat =  new MateriaDto(); 
            mat.setNome(rs.getString("nome"));
            mat.setId(rs.getInt("id"));
            mat.setCarga(rs.getInt("carga"));
            listMat.add(mat);
         }
         stmt.close();
         return listMat;
         
    }
    public void delMateria(int id) throws SQLException{
        PreparedStatement stmt = connection.prepareStatement(sqldel);
        stmt.setInt(1, id);
        stmt.execute();
        stmt.close();
        System.out.println("aqui");
    }
    
    
}
